export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

export const formatDate = timestamp => {
  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('.');
}

export const formatDateStr = () => {
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();

  return year+''+month+''+day;
}

export const formatTimeWithoutYear = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export function spliteSpecString(subTitle) {
  if (subTitle == null || subTitle.length == 0) {
    return null;
  }

  let subTitles = [];
  let startIndex = 0;
  while (startIndex < subTitle.length) {
    let index = subTitle.indexOf('$', startIndex);
    if (index == -1 || index == subTitle.length - 1) {
      let subStr = subTitle.substr(startIndex);
      if (subStr.length > 0) {
        subTitles.push({
          spec : false,
          text: subStr
        });
      }
      break;
    }

    let index2 = subTitle.indexOf('$', index + 1);
    if (index2 == -1) {
      subTitles.push({
        spec: false,
        text: subTitle.substr(startIndex)
      });
      break;
    }

    let preString = subTitle.substr(startIndex, index - startIndex);
    if (preString != null && preString.length > 0) {
      subTitles.push({
        spec: false,
        text: preString
        });
    }


    let specStr = subTitle.substr(index + 1, index2 - index - 1);
    if (specStr != null && specStr.length > 0) {
      subTitles.push({
        spec: true,
        text: specStr
      });
    }

    startIndex = index2 + 1;
  }

  return subTitles.length > 0 ? subTitles : null;
}

/**
   * 格式化倒计时 01:02:03 -> ['01', '02', '03.1']
   */
export function formatCountDown(countdown, baseMS) {
  let seconds = Math.floor(countdown / 1000);
 
  let ms = '';
  if (baseMS > 0 && baseMS < 1000) {
    ms = '.' + Math.floor((countdown - seconds * 1000) / baseMS);
  }

  let hour = Math.floor(seconds / 3600);
  seconds -= hour * 3600;
  let minite = Math.floor(seconds / 60);
  seconds = seconds - minite * 60;

  let ret = [];
  ret.push(hour > 9 ? '' + hour : '0' + hour);
  ret.push(minite > 9 ? '' + minite : '0' + minite);
  ret.push(seconds > 9 ? '' + seconds + ms : '0' + seconds + ms);
  return ret;
}

export function formatCountDownDays(countdown) {
  let day = Math.floor(countdown / 86400);
  countdown -= day * 86400;
  let hour = Math.floor(countdown / 3600);
  countdown -= hour * 3600;
  let minite = Math.floor(countdown / 60);
  let seconds = countdown - minite * 60;
  if (day > 0) {
    return day + '天' + hour + '小时' + minite + '分' + seconds + '秒';
  }

  if (hour > 0) {
    return hour + '小时' + minite + '分' + seconds + '秒';
  }

  if (minite > 0) {
    return minite + '分' + seconds + '秒';
  }

  return seconds + '秒';
}

export function formatFloat(f, c) {
  let numString = f.toString();
  let index = numString.indexOf(".");
  if (index != -1) {
    let endString = numString.substr(index);
    if (endString.length > c + 1) {
      endString = endString.substr(0, c + 1);
    }

    if (c == 0 || (c == 1 && endString == '.0') || (c == 2 && endString == '.00') || (c == 3 && endString == '.000')) {
      endString = '';
    }
		

    numString = numString.substr(0, index) + endString;
  }
  return numString;
}

export function formatMoney(money, c) {
  money = Math.ceil(money * 100) / 100;
  let str = money.toString();
  let index = str.indexOf(".");
  if (index == -1 && c > 0) {
    str += c == 1? '.0' : '.00';
  } else if (c == 2 && index + 2 == str.length) {
      str += '0';
  }
  return str;
}

export function today() {
  return formatDateToNum(new Date())
}

export function formatDateToNum(date) {
  return date.getFullYear().toString() + 
    ("00" + (date.getMonth() + 1).toString()).slice(-2) + 
    ("00" + date.getDate().toString()).slice(-2);
}

// module.exports = {
//   formatTime: formatTime,
//   formatDate: formatDate,
//   formatNumber: formatNumber,
//   spliteSpecString: spliteSpecString,
//   formatCountDown: formatCountDown,
//   formatCountDownDays: formatCountDownDays,
//   formatFloat: formatFloat,
//   formatMoney: formatMoney,
//   formatTimeWithoutYear: formatTimeWithoutYear,
//   today: today,
//   formatDateToNum: formatDateToNum,
//   formatDateStr: formatDateStr
// }
